<?php
include 'validationfunctions.php';
include 'savegoalfunction.php';
include 'deletegoalfunction.php';
include 'updategoalfunction.php';
#session_start();
$output_form = false;
if (isset($_POST['submit'])){

	if (isset($_GET['goalid'])){
		$goalid = $_GET['goalid'];
	}
	
	$goalname = $_POST['goal'];
	$measurename = $_POST['measure_value'];			
	if (isset($_POST['measuretype_value'])){
		$measuretype = $_POST['measuretype_value'];
	}else{
		$measuretype = "5";
	}			
	$startvalue = $_POST['start_value'];
	$currentvalue = $_POST['value'];
	$targetvalue = $_POST['target_value'];
	$priority = $_POST['prio'];
	$deadline = $_POST['deadline'];
	$description = $_POST['description'];
	
	$errorListRequieredFields = array();
	$errorGoalNameExists = "";
	$errorEqualsSubgoalNamesExist = "";
	$errorListDeadlines = array();
	$errorListGoalMeasureValues = array();
	$errorListSubgoalValues = array();
			
	$errorListRequieredFields = validateRequiredFields(true);
	$errorGoalNameExists = validateExistingGoalNameOnEditPage($goalid, $goalname);
	$errorEqualsSubgoalNamesExist = validateEqualSubgoalNames();
	$errorListDeadlines = validateDeadlines();
	
	$startValue = $_POST['start_value'];
	$currentValue = $_POST['value'];
	$targetValue = $_POST['target_value'];
	$errorListGoalMeasureValues = validateGoalValues($measuretype, $startValue, $currentValue, $targetValue);			
	$errorListSubgoalValues = validateSubgoalsValuesOnEditPage();
	$showValidationErrors = false;
			
	if (count($errorListRequieredFields) == 0 &&
	 			$errorGoalNameExists == "" &&
	  			$errorEqualsSubgoalNamesExist == "" &&
				count($errorListDeadlines) == 0 &&
				count($errorListGoalMeasureValues) == 0 &&
				count($errorListSubgoalValues) == 0){
		updateGoal($goalid, $goalname,$description,$startvalue,$currentvalue,$targetvalue,$priority,$measurename,$measuretype, $deadline);
		header("Location:index.php?page=overview&back=1");
		ob_end_flush();
	}else{
		$showValidationErrors = true;
		$output_form = true;	
			# show form again
		ob_end_flush();
	}
	ob_end_flush();			
	if ($output_form == true){			
		?>
				
		<body onload="preventChangesOfGoalValues();">
			<div id="wrapper">
				<header>
					<a href="index.php?logout=true">Log out</a>
				</header>
				<div id="content">
					<h1><?php echo "Edit \"".$goalname."\""; ?></h1>
					<?php
						if($showValidationErrors = true){
								echo "<div class=\"errors\">";
								echo "<p>";
								foreach ($errorListRequieredFields as $i => $value) { 
									echo $errorListRequieredFields[$i].'<br/>';
								}
								
								if($errorGoalNameExists != "" || $errorEqualsSubgoalNamesExist != ""){
									echo $errorGoalNameExists.'<br/>';
									echo $errorEqualsSubgoalNamesExist.'<br/>';
								}				
								foreach ($errorListDeadlines as $i => $value) {
									echo $errorListDeadlines[$i].'<br/>';
								}
								
								foreach ($errorListGoalMeasureValues as $i => $value) {
									echo $errorListGoalMeasureValues[$i].'<br/>';
								}
								foreach ($errorListSubgoalValues as $i => $value) {
									echo $errorListSubgoalValues[$i].'<br/>';
								}
								echo "</p>";
								echo "</div>";	
							
						}
					
					?>
					<form action="<?php echo $_SERVER['PHP_SELF']."?page=edit&goalid=".$goalid; ?>" method="post">
						<br />
						
						<label>Goal *</label>
						<input type="text" name="goal" value="<?php echo $goalname;?>"/>
						<br />
						<label>Description </label>
						<textarea name="description" type="text"><?php echo $description; ?></textarea>
						<br />
						<label>Deadline </label>
						<input type="date" name="deadline" value="<?php echo $deadline ?>"/>
						<br />
						<label>Priority *</label>
						<select name="prio">
							<option name="urgimp" value="ui" <?php if($priority == "ui"){echo " selected";}?>>1 - urgent / important</option>
							<option name="urgnimp" value="uni" <?php if($priority == "uni"){echo " selected";}?>>2 - urgent / unimportant</option>
							<option name="nurgimp" value="nui" <?php if($priority == "nui"){echo " selected";}?>>3 - unurgent / important</option>
							<option name="nurgnimp" value="nuni" <?php if($priority == "nuni"){echo " selected";}?>>4 - unurgent / unimportant</option>
						</select>
						<br />
						
						<label>Start Value *</label>
						<input id="startvalue" type="text" name="start_value" value="<?php echo $startvalue;?>" <?php if(sizeof($_POST) > 12){echo "readonly=\"readonly\"";}?>/>
						<br />
						<label>Current Value *</label>
						<input id="currentvalue" type="text" name="value" value="<?php echo $currentvalue;?>" <?php if(sizeof($_POST) > 12){echo "readonly=\"readonly\"";}?>/>
						<br />
						<label>Target value *</label>
						<input id="targetvalue" type="text" name="target_value" value="<?php echo $targetvalue;?>" <?php if(sizeof($_POST) > 12){echo "readonly=\"readonly\"";}?>/>
						<br />
						<label>Measure name *</label>
						<input id="measurename" type="text" name="measure_value" value="<?php echo $measurename?>" <?php if(sizeof($_POST) > 12){echo "readonly=\"readonly\"";}?>/>
						<br />
						<label>Measure type *</label>										
						<select id="measuretype" name="measuretype_value" onchange="goalTypeChosen(this);">>
								<option name="integer" value="1" <?php if($measuretype == "1"){echo "selected";}?>>integer</option>
								<option name="float" value="2" <?php if($measuretype == "2"){echo "selected";}?>>float</option>
								<option name="money" value="3" <?php if($measuretype == "3"){echo "selected";}?>>money</option>
								<option name="yesno" value="4" <?php if($measuretype == "4"){echo "selected";}?>>yes/no</option>
								<option name="percent" value="5" <?php if($measuretype == "5"){echo "selected";}?>>percent</option>
						</select>
						<br />
						<br />
						<input type="button" value="add new subgoal" onclick="addsubgoalOnEditPage()"/>
						<br />
						
						
						<!-- Tables for Subgoals -->
						<table class="colored">
							<thead>							
								<th>Subgoal*</th>
								<th>Deadline</th>
								<th>Priority*</th>							
								<th>Start Value*</th>
								<th>Current Value*</th>
								<th>Target Value*</th>
								<th>Measure Name*</th>
								<th>Measure Type*</th>
								<th></th>
							</thead>
							<tbody id="subgoalstbody">							
		<?php
		# read POST	
		foreach($_POST as $key=>$value) {
			if (startsWith($key, "subgoalname")){
				$splitted = preg_split('/[_]/', $key);
				$subgoalid = $splitted[1];
				
				$subgoalname = $value;
				$subgoalstartvalue = $_POST["subgoalstart_$subgoalid"];
				$subgoalcurrentvalue = $_POST["subgoalcurrent_$subgoalid"];
				$subgoaltargetvalue = $_POST["subgoaltarget_$subgoalid"];
				$subgoalpriority = $_POST["subgoalpriority_$subgoalid"];
				$subgoalmeasurename = $_POST["subgoalmeasurename_$subgoalid"];
				$subgoaldeadline= $_POST["subgoaldeadline_$subgoalid"];
				$subgoalmeasuretype = $_POST["subgoalmeasuretype_$subgoalid"];
				switch ($subgoalpriority) {
				case "ui":
					$subgoalpriority = "urgent / important";
					break;
				case "uni":
					$subgoalpriority = "urgent / unimportant";
					break;
				case "nui":
					$subgoalpriority = "unurgent / important";
					break;
				case "nuni":
					$subgoalpriority = "unurgent / unimportant";
					break;
				} # switch?> 
				<!--Add line -->	
				<tr id="trsubgoal_<?php echo $subgoalid;?>" name="trsubgoal_<?php echo $subgoalid;?>">								
					<td><input type="text" name="subgoalname_<?php echo $subgoalid;?>" value="<?php echo $subgoalname;?>"/></td>
					<td><input type="date" name="subgoaldeadline_<?php echo $subgoalid;?>" value="<?php echo $subgoaldeadline;?>"/></td>
					<td>
						<select id="subgoalpriority_<?php echo $goalid;?>" name="subgoalpriority_<?php echo $subgoalid;?>">
							<option name="urgimp" value="ui" <?php if($subgoalpriority == "urgent / important"){echo " selected";}?>>1 - urgent / important</option>
							<option name="urgnimp" value="uni" <?php if($subgoalpriority == "urgent / unimportant"){echo " selected";}?>>2 - urgent / unimportant</option>
							<option name="nurgimp" value="nui" <?php if($subgoalpriority == "unurgent / important"){echo " selected";}?>>3 - unurgent / important</option>
							<option name="nurgnimp" value="nuni" <?php if($subgoalpriority == "unurgent / unimportant"){echo " selected";}?>>4 - unurgent / unimportant</option>
						</select>
					</td>
					<td><input id="subgoalstart_<?php echo $subgoalid; ?>" name="subgoalstart_<?php echo $subgoalid; ?>" type="text" value="<?php echo $subgoalstartvalue;?>"/></td>								
					<td><input id="subgoalcurrent_<?php echo $subgoalid; ?>" name="subgoalcurrent_<?php echo $subgoalid; ?>" type="text" value="<?php echo $subgoalcurrentvalue;?>"/></td>
					<td><input id="subgoaltarget_<?php echo $subgoalid; ?>" name="subgoaltarget_<?php echo $subgoalid; ?>" type="text" value="<?php echo $subgoaltargetvalue;?>"/></td>
					<td><input id="subgoalmeasurename_<?php echo $subgoalid; ?>" name="subgoalmeasurename_<?php echo $subgoalid; ?>" type="text" value="<?php echo $subgoalmeasurename;?>"/></td>
					<td>
						<select id="select_<?php echo $subgoalid; ?>" name="subgoalmeasuretype_<?php echo $subgoalid; ?>" onchange="subgoalTypeChosen(this);">
							<option name="integer" value="1" <?php if($subgoalmeasuretype == "1"){echo "selected";}?>>integer</option>
							<option name="float" value="2" <?php if($subgoalmeasuretype == "2"){echo "selected";}?>>float</option>
							<option name="money" value="3" <?php if($subgoalmeasuretype == "3"){echo "selected";}?>>money</option>
							<option name="yesno" value="4" <?php if($subgoalmeasuretype == "4"){echo "selected";}?>>yes/no</option>
							<option name="percent" value="5" <?php if($subgoalmeasuretype == "5"){echo "selected";}?>>percent</option>
						</select>
					</td>
					<td>
					<input type="button" value="delete" onclick="deleteSubgoalOnEditPage(<?php echo $subgoalid;?>)"/>
					</td>
				</tr>
		<?php }# end of foreach?> 
	<?php }#end of if für subgoal kleines IF ?>
					</tbody>
				</table>
				<br />
				<input type="button" value="add new subgoal" onclick="addsubgoalOnEditPage()" />
				<br />
				<br />
				<input type="submit" name="submit" value="Save changes" />
			</form>
			<br/>
			<a href="index.php?page=overview&back=1">goals overview</a>
		</div>
		<footer></footer>
	</div>
</body>
		<?php	}}
			
	if($output_form == false){# not submitted
		mysql_connect("localhost", "web964","pass17") or die ("No connection to database.");
		mysql_select_db("usr_web964_1") or die ("Database does not exist.");

		$result1 = mysql_query("SELECT * FROM goal WHERE id='$goalid'") or die ("MySQL Error");
		$goaldata = array();
		while ($row = mysql_fetch_object($result1)) {
			$goaldata['id'] = $row->id;
			$goaldata['name'] = $row->name;
			$goaldata['description'] = $row->description;
			$goaldata['priority'] = $row->priority;
			$goaldata['measurename'] = $row->measurename;
			$goaldata['value'] = $row->value;
			$goaldata['startvalue'] = $row->startvalue;
			$goaldata['targetvalue'] = $row->targetvalue;
			$goaldata['fk_measuretypeid'] = $row->fk_measuretypeid;
		}
		
		$goaldata['goaldeadline'] ="";
		$result2 = mysql_query("SELECT date FROM goaldeadline WHERE fk_goalid='$goalid'") or die ("MySQL Error");
		while ($row2 = mysql_fetch_object($result2)) {
			$goaldata['goaldeadline'] = $row2->date;
		}
		
		$result3 = mysql_query("SELECT id FROM measuretype WHERE id='".$goaldata['fk_measuretypeid']."'") or die ("MySQL Error");
		while ($row3 = mysql_fetch_object($result3)) {
			$goaldata['goalmeasuretype'] = $row3->id;
		}
	?>
		
	<body onload="preventChangesOfGoalValues();">
		<div id="wrapper">
			<header>
				<a href="index.php?logout=true">Log out</a>
			</header>
			<div id="content">
				<h1><?php echo "Edit \"".$goaldata['name']."\""; ?></h1>
				
				<form action="<?php $_SERVER['PHP_SELF']."?page=edit&goalid=".$goalid; ?>" method="post">
					<br />
					
					<label>Goal *</label>
					<input type="text" name="goal" value="<?php echo $goaldata['name'];?>"/>
					<br />					
					<label>Description </label>
					<textarea name="description" type="text"><?php echo $goaldata['description']; ?></textarea>
					<br />
					<label>Deadline (YYYY-MM-DD) </label>
					<input type="date" name="deadline" value="<?php echo $goaldata['goaldeadline'];?>"/>
					<br />
					<label>Priority *</label>
					<select name="prio">
						<option name="urgimp" value="ui" <?php if($goaldata['priority'] == "ui"){echo " selected";}?>>1 - urgent / important</option>
						<option name="urgnimp" value="uni" <?php if($goaldata['priority'] == "uni"){echo " selected";}?>>2 - urgent / unimportant</option>
						<option name="nurgimp" value="nui" <?php if($goaldata['priority'] == "nui"){echo " selected";}?>>3 - unurgent / important</option>
						<option name="nurgnimp" value="nuni" <?php if($goaldata['priority'] == "nuni"){echo " selected";}?>>4 - unurgent / unimportant</option>
					</select>
					<br />
					<label>Start Value *</label>
					<input id="startvalue" type="text" name="start_value" value="<?php echo $goaldata['startvalue'];?>"/>
					<br />
					<label>Current Value *</label>
					<input id="currentvalue" type="text" name="value" value="<?php echo $goaldata['value'];?>"/>
					<br />
					<label>Target value *</label>
					<input id="targetvalue" type="text" name="target_value" value="<?php echo $goaldata['targetvalue'];?>"/>
					<br />
					<label>Measure name *</label>
					<input id="measurename" type="text" name="measure_value" value="<?php echo $goaldata['measurename'];?>"/>
					<br />
					<label>Measure type *</label>
					
					<?php 
						$result4 = mysql_query("SELECT * FROM subgoal WHERE fk_goalid='".$goalid."'") or die ("MySQL Error");
					?>
					
					<select id="measuretype" name="measuretype_value" onchange="goalTypeChosen(this);" <?php echo ((mysql_num_rows($result4) == 0)? "": "disabled"); ?>>
							<option name="integer" value="1" <?php if($goaldata['goalmeasuretype'] == "1"){echo "selected";}?>>integer</option>
							<option name="float" value="2" <?php if($goaldata['goalmeasuretype'] == "2"){echo "selected";}?>>float</option>
							<option name="money" value="3" <?php if($goaldata['goalmeasuretype'] == "3"){echo "selected";}?>>money</option>
							<option name="yesno" value="4" <?php if($goaldata['goalmeasuretype'] == "4"){echo "selected";}?>>yes/no</option>
							<option name="percent" value="5" <?php if($goaldata['goalmeasuretype'] == "5"){echo "selected";}?>>percent</option>
					</select>
					<br />
					<br />
					<input type="button" value="add new subgoal" onclick="addsubgoalOnEditPage()"/>
					<hr />
					
					
					<!-- Tables for Subgoals -->
					<table class="colored">
						<thead>							
							<th>Subgoal*</th>
							<th>Deadline</th>
							<th>Priority*</th>							
							<th>Start Value*</th>
							<th>Current Value*</th>
							<th>Target Value*</th>
							<th>Measure Name*</th>
							<th>Measure Type*</th>
							<th></th>
						</thead>
						<tbody id="subgoalstbody">							
							<?php								
								while ($row4 = mysql_fetch_object($result4)) { 
									# query for subgoal deadline
									$subgoaldeadline = "";
									$result6 = mysql_query("SELECT date FROM subgoaldeadline WHERE fk_subgoalid='".$row4->id."'") or die ("MySQL Error");
									while ($row6 = mysql_fetch_object($result6)){
										$subgoaldeadline = $row6->date;
									}
									
									?>
									
									
									<tr id="trsubgoal_<?php echo $row4->id;?>" name="trsubgoal_<?php echo $row4->id;?>">								
										<td><input type="text" name="subgoalname_<?php echo $row4->id;?>" value="<?php echo $row4->name;?>"/></td>
										<td><input type="date" name="subgoaldeadline_<?php echo $row4->id;?>" value="<?php echo $subgoaldeadline;?>"/></td>
										<td>
											<select name="subgoalpriority_<?php echo $row4->id;?>">
												<option name="urgimp" value="ui" <?php if($row4->priority == "urgent / important"){echo " selected";}?>>1 - urgent / important</option>
												<option name="urgnimp" value="uni" <?php if($row4->priority == "urgent / unimportant"){echo " selected";}?>>2 - urgent / unimportant</option>
												<option name="nurgimp" value="nui" <?php if($row4->priority == "unurgent / important"){echo " selected";}?>>3 - unurgent / important</option>
												<option name="nurgnimp" value="nuni" <?php if($row4->priority == "unurgent / unimportant"){echo " selected";}?>>4 - unurgent / unimportant</option>
											</select>
										</td>
										<td><input id="subgoalstart_<?php echo $row4->id; ?>" name="subgoalstart_<?php echo $row4->id; ?>" type="text" value="<?php echo $row4->startvalue;?>"/></td>								
										<td><input id="subgoalcurrent_<?php echo $row4->id; ?>" name="subgoalcurrent_<?php echo $row4->id; ?>" type="text" value="<?php echo $row4->value;?>"/></td>
										<td><input id="subgoaltarget_<?php echo $row4->id; ?>" name="subgoaltarget_<?php echo $row4->id; ?>" type="text" value="<?php echo $row4->targetvalue;?>"/></td>
										<td><input id="subgoalmeasurename_<?php echo $row4->id; ?>" name="subgoalmeasurename_<?php echo $row4->id; ?>" type="text" value="<?php echo $row4->measurename;?>"/></td>
										<td>
											<select id="select_<?php echo $row4->id; ?>" name="subgoalmeasuretype_<?php echo $row4->id; ?>" onchange="subgoalTypeChosen(this);">
												<option name="integer" value="1" <?php if($row4->fk_measuretypeid == "1"){echo "selected";}?>>integer</option>
												<option name="float" value="2" <?php if($row4->fk_measuretypeid == "2"){echo "selected";}?>>float</option>
												<option name="money" value="3" <?php if($row4->fk_measuretypeid == "3"){echo "selected";}?>>money</option>
												<option name="yesno" value="4" <?php if($row4->fk_measuretypeid == "4"){echo "selected";}?>>yes/no</option>
												<option name="percent" value="5" <?php if($row4->fk_measuretypeid == "5"){echo "selected";}?>>percent</option>
											</select>
										</td>
										<td>
										<input type="button" value="delete" onclick="deleteSubgoalOnEditPage(<?php echo $row4->id;?>)"/>
										</td>
									</tr>
							<?php	}	#end of while loop ?>
											
						</tbody>
					</table>
					<hr />
					<input type="button" value="add new subgoal" onclick="addsubgoalOnEditPage()" />
					<br />
					<br />
					<input type="submit" name="submit" value="Save changes" />
				</form>
				<br/>
				<a href="index.php?page=overview&back=1">goals overview</a>
			</div>
			<footer></footer>
		</div>
	</body>
<?php } # end of else?>

